

function checkPhone(phone) {
    if(phone.search(/^1[3458]{1}[0-9]{9}$/) == -1)
        return false;
    else
        return true;
}

function checkQq(qq) {
    if(qq.search(/[1-9]\d{4,}/) == -1)
        return false;
    else
        return true;
}

var sina = {
	status: function() {
        var status = window.getUrlParam('status');
        var code = window.getUrlParam('code');
        if(status == 'true') {

            $.ajax({
                url: window.domain + window.requestUrl.checkStatuswbUrl,
                type: 'post',
                xhrFields: {
                    withCredentials: true
                },
                crossDomain: true,
                data: {
                    code: code
                },
                success: function(res) {
                    if(typeof(res.not_login) != 'undefined') {
                        window.setCookie('go_back_link', window.location.href);
                        window.location.href = './login.html';
                        return;
                    }
                    if(res.status) {
                        $('.mediaLogined-form').show();
                        var result = res.data.info;
                        $('[data-role="iconUrl"]').attr('src', result.iconUrl);
                        $('[data-role="name"]').html(result.title);
						$('[data-role="fans"]').html(result.fans);


                    } else {
                        iosOverlay({
                            text: res.msg,
                            duration: 1000
                        });
                    }
                },
                error: function(e) {
                }
            })
        } else {
            if(code == 3) {
                window.wxc.xcConfirm('该账号已入库，退出当前账号，添加其他账号', null, {
                    title: '提示'
                });
                $('.sgBtn.ok').prop('href', 'http://www.weibo.com').prop('target', '_blank');
                $('.sgBtn.ok').click(function() {
                    $('.xcConfirm').remove();
                })
            } else {
                iosOverlay({
                    text: decodeURIComponent(window.getUrlParam('msg')),
                    duration: 3000,
                });
            }
        }
    },
    submit: function() {
        $(document).on('click', '[data-role="submit"]', function() {

            var qq = $('[data-role="qq"]').val();
            var $qq = $('[data-role="qq"]');
            if(qq == '' || !checkQq(qq)) {
                iosOverlay({
                    text: '请填写正确的QQ',
                    duration: 1000
                });
                return;
            }

            var ttbj = $('[data-role="ttbj"]').val();
            var $ttbj = $('[data-role="ttbj"]');
            var ctbj = $('[data-role="ctbj"]').val();
            var $ctbj = $('[data-role="ctbj"]');
            if(ttbj == '' && ctbj == '') {
                iosOverlay({
                    text: '直发价格和转发价格至少填写一项',
                    duration: 1000
                });
                return;
            }

            if($('.plus-tag a').length == 0) {
                iosOverlay({
                    text: '请选择分类标签',
                    duration: 1000
                });
                return;
            }



            if(checkQq(qq) && (ttbj != '' || ctbj != '') && $('.plus-tag a').length > 0) {
                var tagArr = [];
                $.each($('.plus-tag a'), function(index, item) {
                    tagArr.push($(item).attr('data-id'));
                })
                var obj = {
                    qq: qq,
                    tagJsonArr: tagArr,
                    positionJsonObj: {"province": $('#province').val(), "city": $('#city').val(), "county": $('#county').val()},
                    price1: ttbj == '' ? -1 : ttbj,
                    price2: ctbj == '' ? -1 : ctbj
                };
                $.ajax({
                    url: window.domain + window.requestUrl.addWbUrl,
                    type: 'post',
                    data: {
                        uParams: JSON.stringify(obj)
                    },
                    xhrFields: {
                        withCredentials: true
                    },
                    crossDomain: true,
                    success: function(res) {
                        if(typeof(res.not_login) != 'undefined') {
                            window.setCookie('go_back_link', window.location.href);
                            window.location.href = './login.html';
                            return;
                        }
                        if(res.status) {
                            window.location.href = './sina-show.html';
                        } else {
                            iosOverlay({
                                text: res.msg,
                                duration: 1000
                            });
                        }
                    },
                    error: function(e) {
                    }
                })
            }
        })
    },
    addTag: function() {
        $.ajax({
            url: window.domain + window.requestUrl.getPlatTagUrl + '5',
            type: 'get',
            xhrFields: {
                withCredentials: true
            },
            crossDomain: true,
            success: function(res) {
                if(typeof(res.not_login) != 'undefined') {
                    window.setCookie('go_back_link', window.location.href);
                    window.location.href = './login.html';
                    return;
                }
                if(res.status) {
                    var tags = res.data.tags;
                    var tpl = '';
                    $.each(tags, function(index, item) {
                        tpl += '<a value="-1" title="'+item.tag+'" data-id="'+item.id+'" href="javascript:void(0);"><span>'+item.tag+'</span><em></em></a>';
                    })
                    $('.default-tag .clearfix').html(tpl);
                } else {
                    iosOverlay({
                        text: res.msg,
                        duration: 1000
                    });
                }
            },
            error: function(e) {
            }
        })
    },
    init: function() {
        this.submit();
        this.addTag();
		this.status();
    }
}
sina.init();
